package club.zhcs.capture.biz.dept.impl;

import static club.zhcs.capture.vo.Consts.SystemValues.PAGESIZE;

import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import club.zhcs.capture.bean.dept.Project;
import club.zhcs.capture.biz.dept.IProjectService;
import club.zhcs.capture.dao.dept.IProjectDao;

import com.dgj.utils.db.SqlActuator;
import com.dgj.utils.entries.Result;
import com.dgj.utils.web.pager.Pager;

@IocBean
public class ProjectService implements IProjectService {

	@Inject
	IProjectDao projectDao;

	@Override
	public Result save(Project project) {
		return projectDao.save(project) == null ? Result.fail("添加项目失败") : Result.success().addData("project", project);
	}

	@Override
	public Pager<Record> listByPage(int page) {
		Sql sql = Sqls
				.create("SELECT p.*,s.sub_name,u.u_real_name FROM gj_project p LEFT JOIN gj_subsidiary s ON p.p_sub_id = s.id LEFT JOIN gj_user u ON p.p_principal_id = u.id LIMIT @pageStart,@pageSize");
		sql.params().set("pageSize", PAGESIZE);
		sql.params().set("pageStart", (page - 1) * PAGESIZE);
		Pager<Record> pager = new Pager<Record>(PAGESIZE, page);
		pager.setEntities(SqlActuator.runReport(sql, projectDao.getDao()));
		pager.setCount(projectDao.countAll());
		return pager;
	}

	@Override
	public Pager<Record> searchByPage(int page, String key) {
		String searchKey = String.format("%%%s%%", key);
		Sql sql = Sqls
				.create("SELECT p.*,s.sub_name,u.u_real_name FROM gj_project p LEFT JOIN gj_subsidiary s ON p.p_sub_id = s.id LEFT JOIN gj_user u ON p.p_principal_id = u.id  where p.p_name like @searchKey LIMIT @pageStart,@pageSize");
		sql.params().set("pageSize", PAGESIZE);
		sql.params().set("pageStart", (page - 1) * PAGESIZE);
		sql.params().set("searchKey", searchKey);
		Pager<Record> pager = new Pager<Record>(PAGESIZE, page);
		pager.setEntities(SqlActuator.runReport(sql, projectDao.getDao()));
		pager.setCount(projectDao.countByCnd(Cnd.where("name", "like", searchKey)));
		return pager;
	}

	@Override
	public boolean delete(int id) {
		return projectDao.delById(id);
	}

	@Override
	public Record getAllInfo(int id) {
		Sql sql = Sqls
				.create("SELECT p.*,s.sub_name,s.id AS s_id,u.u_real_name,u.id AS u_id FROM gj_project p LEFT JOIN gj_subsidiary s ON p.p_sub_id = s.id LEFT JOIN gj_user u ON p.p_principal_id = u.id  where p.id = @id");
		sql.params().set("id", id);
		List<Record> records = SqlActuator.runReport(sql, projectDao.getDao());
		return records == null ? null : records.get(0);
	}

	@Override
	public Result edit(Project project) {
		return !projectDao.update(project) ? Result.fail("更新分支机构失败!") : Result.success().addData("project", project);
	}

	@Override
	public List<Project> listAll() {
		return projectDao.findAll();
	}

	@Override
	public List<Project> listBySubsidiaryId(int id) {
		return projectDao.search(Cnd.where("subsidiaryId", "=", id));
	}
}
